package com.lagou.v4.coyote;

import com.lagou.v4.LifeCycle;
import com.lagou.v4.container.Service;

/**
 * Minicat连接器组件，负责监听Socket请求，将请求的输入流信息封装成Container容器所需的ServletRequest对象
 *
 * @author Kirito
 * @description:
 * @date 2020/04/29
 */
public class Connector implements LifeCycle {

    private Integer port;

    private EndPoint endPoint;

    private Service service;

    private CoyoteAdaptor adaptor;

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    @Override
    public void init() throws Exception {
        endPoint = getEndPoint();

        adaptor = new CoyoteAdaptor(this);
        endPoint.setAdaptor(adaptor);

        endPoint.init();
    }

    @Override
    public void start() throws Exception {
        endPoint.start();
    }

    private EndPoint getEndPoint() {
        EndPoint endPoint = new EndPoint();
        endPoint.setPort(port);
        return endPoint;
    }
}
